package com.tjw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tjw.constant.MessageConstant;
import com.tjw.constant.RedisConstant;
import com.tjw.entity.PageResult;
import com.tjw.entity.QueryPageBean;
import com.tjw.entity.Result;
import com.tjw.pojo.Setmeal;
import com.tjw.service.SetMealService;
import com.tjw.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * @author tjw
 * @Date 2020/10/9
 */
@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setMealService.pageQuery(queryPageBean);
    }

    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();  //原始文件名
        if(originalFilename!=null && originalFilename.length()>0) {
            int index = originalFilename.lastIndexOf(".");
            String suffix = originalFilename.substring(index);
            String fileName = UUID.randomUUID().toString() + suffix;
            //将图片保存到七牛云
            try {
                QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
                //文件上传成功后，需要将文件名称保存到redis中
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
            }
        }
        return null;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setMealService.add(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer id){
        try {
            Integer[] list = setMealService.findCheckGroupById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setMealService.edit(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            setMealService.delete(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
}
